using Best.WebSockets;
using Client.Hotfix;
using System;

namespace Client.Base
{
    public class WSService<T> where T : WSSession, new()
    {
        private WebSocket webSocket;
        public bool IsConnected => webSocket!=null && webSocket.State == Best.WebSockets.Implementations.WebSocketStates.Open;
        public T session;
        public void BuildClient(string wss)
        {
            webSocket = new WebSocket(new Uri(wss));
            StartConnect();
        }

        private void StartConnect()
        {
            session = new T();
            session.InitSession(webSocket);
            webSocket.Open();
        }

        public void CloseClient()
        {
            session?.CloseSession();
            webSocket.Close();
            session = null;
            webSocket = null;
        }
    }
}

